//class PlaneStrengthen
var config = [
    '战机伤害增加:{0}%',
    '暴走持续时长增加{0}秒',
    '游戏开始直接携带闪电{0}个',
    '必杀的伤害增加:{0}%'
]

cc.Class({
    extends: require('UI-PanelWindow'),

    properties: {
        item: [cc.Node],
        panel: cc.Node,
        tips2: cc.Label,
        levelShow: [cc.ProgressBar],
        coinNumber: cc.Label,
        coins: cc.Label,
        btn: cc.Node,
        userInfo: require('UserInfo'),
        energy: cc.Node
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        SoundMgr.playMusic('./Audio/bgm_sj')
        this._super();
        this.userInfo.setOptions(global.Data)
        this.arr = ['childArms', 'childArmsLevel', 'healthLevel']
        this.arr2 = ['mainArms', 'childArms', 'health']
        this.arr3 = ['主武器伤害提升到', '副武器伤害提升到', '血量增加到']
        this.updateCoin = function () {
            this.coins.string = global.Data.coin;
        }.bind(this)
        global.Event.on('COIN', this.updateCoin);
        this.checkLevel()
        this.updateUI(0);
    },

    checkLevel() {
        for (let i = 0; i < this.levelShow.length; i++) {
            let j = i == 2 ? 3 : i;
            this.levelShow[i].progress = global.Data.plane[this.arr[j]] / 100;
        }
    },

    updateUI(type) {
        this.coins.string = global.Data.store.goldCoin;
        this.type = parseInt(type);
        let level = global.Data.plane[this.arr[this.type]];
        for (var i = 0; i < 4; i++) {
            this.item[i].children[2].active = false;
        }
        this.item[this.type].children[2].active = true;
        var animCtrl = this.item[this.type].children[2].getComponent(cc.Animation);
        var animCtrl1 = this.panel.getComponent(cc.Animation);
        animCtrl.play("levelUp");
        animCtrl1.play("showPanel");

        this.upCoin = 0;
        if (this.type == 2) {
            let maxlevel = 4;
            level = level > 4 ? 4 : level;
            this.btn.children[0].active = level < maxlevel
            this.btn.children[1].active = level >= maxlevel
            this.upCoin = maxlevel == level ? 0 : global.Data.upgrade[4][level];
            this.coinNumber.string = this.upCoin;
            let up = global.Data.upgrade[this.type]
            this.tips2.string = '当前等级' + level + '\n' + config[this.type].format(up[Math.min(maxlevel - 1, level)]);
            this.energy.children[0].active = global.Data.plane.skillLevel >= 1 ? true : false
            this.energy.children[1].active = global.Data.plane.skillLevel >= 2 ? true : false
            this.energy.children[2].active = global.Data.plane.skillLevel >= 3 ? true : false
            this.energy.children[3].active = global.Data.plane.skillLevel >= 4 ? true : false
        } else {
            let number = this.type
            let maxlevel = 100
            this.btn.children[0].active = level < maxlevel
            this.btn.children[1].active = level >= maxlevel
            this.upCoin = level == maxlevel ? 0 : (level + 1) * 1;
            this.coinNumber.string = this.upCoin;
            this.tips2.string = level == maxlevel ? '已满级' : '当前等级' + level + '\n' + this.arr3[number] + (level + 1) + '%';
        }
        this.checkLevel();
    },

    //基础按钮回调
    onClick(event, data) {
        console.log(data);
        if (data == 'back') {
            this.removeFromUI(event, 1);
        } else if (data == 'hint') {
            this._onClickHint();
        } else if (data == 'levelUp') {
            this._levelUp();
        }
    },

    onClickSelectType(event, type) {
        this.type = type;
        this.updateUI(type);
    },

    onDestroy() {
        global.Event.off('COIN')
    },
    coinss(data) {
        this.coins.string = global.Data.store.goldCoin;
    },
    hideSelect() {
        for (let i = 0; i < this.item.length; i++) {
            let self = this.item[i]
            self.children[1].active = false
        }
    },

    _onClickHint() {
        global.EJMgr.showToast('暂未实现')
    },

    _levelUp() {
        var self = this;
        this.httpGet('/game/gameUser', 'plane.upgradeNew', { id: this.type }, function (err, res) {
            if (!err) {
                SoundMgr.playSound('Audio/levelUp')
                global.Data.coin = res.coin;
                global.Data.plane = res.plane
                self.updateUI(self.type);
                global.EJMgr.showToast('升级成功')
            } else {
                global.EJMgr.pushUI('Eject/EveryDayGift')
            }
        })
    }
    // update (dt) {},
});
